<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>题集1</title>
</head>
<body>
    
</body>

<script>
    // &&短路 与 || 刚好相反 ；
    var a = 0, c ='c' , b= 2 ;
     let 或 = a || c ;
    let 与 = (a && c );
    console.log(或,与, 1);

    // 变量提升，也是作用域内的变量提升
    var foo1 = 1
    ;(function() {
        console.log(foo1); // undefined变量提升在函数作用域内没了
        var foo1 = 3 ;
        console.log(foo1);

    })()

    /*反devtool  自己写一个log ,log里面的变量给它设置为访问器属性 ， 一旦log就会触发访问器 只有打开devtool才会触发log
        第二种就是 log 一个日期对象， 重写它的toString()方法, 一旦log 就会调用
    */
    const info = {} ;
    const prototype = Object.getPrototypeOf(info)
    const oldTostring = info.__proto__.toString ;
    prototype.toString = function(...arg){
        alert('3')
        return oldTostring(...arg)
    }
    console.log(info)

/* 连续赋值  右边的不会影响到左边的 */
    {
        let a = { b: 9}, a2 = a ;
        a.c  = a = {x:10 }
        console.log( a, a2)
    }
/* 自增运算  右边的会影响到右边的 */
{let n = 0, b = ++n + n++ ;

}
/* symbol模拟私有属性 方法 */
let  bookNum = Symbol('图书数量'), delBook = Symbol('清除图书')
class Book{
    constructor(){
        this[bookNum] = 0;
    }
    addBook(){
        this[bookNum]++
    }
    [delBook](){
        this[bookNum]--

    }
    getTotal(){
        return this[bookNum]
    }

}

const book12 = new Book() 
console.log(book12)

    var MyLinkedList = function() {
    this.hair=  new Node('h',null, null);
    this.tail = new Node('t', null, this.hair)
    this.hair.next = this.tail
    this.size = 0
};
class Node{
    constructor(val, next,pre){
        this.val =val;
        this.pre =pre;
        this.next =next;
    }
    insertBefore(val){
        const node = new Node(val, this,this.pre);
        this.pre &&  (this.pre.next = node);
        this.pre = node ;
    }
    pushBack(val){
        const node = new Node(val, this.next,this);
        this.next &&(this.next.pre = node);
        this.next = node
    }
    ease(){
        /* 这里必须有pre 和 next */
        this.pre.next = this.next
        this.next.pre = this.pre
    }
}

/** 
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function(index) {
if(index< 0 || index >this.size-1)return -1 ;
let res = this.hair ;
while(index> -1){
    index--
    res = res.next
}
console.log(this.hair, 'get')
return res.val
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function(val) {
this.hair.pushBack(val) ;
console.log(this.hair, 'addhead')
this.size++
return this.hair.next 
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function(val) {
    this.tail.insertBefore(val)
    console.log(this.hair, 'addtail')
this.size++
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function(index, val) {
if( index >this.size)return -1 ;
if(index <= -1){ index =0}
let res = this.hair ;
while(index> -1){
    index--
    res = res.next
}    
res.insertBefore(val)
console.log(this.hair, 'addindex')
this.size++
};

/** 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function(index) {
if(index < 0 | index > this.size -1){ return false}
let res = this.hair
while(index > -1){
    index--
    res = res.next
}
res.ease() 
console.log(this.hair, 'del ind')
this.size--
};

/**
 * Your MyLinkedList object will be instantiated and called as such:
 * var obj = new MyLinkedList()
 * var param_1 = obj.get(index)
 * obj.addAtHead(val)
 * obj.addAtTail(val)
 * obj.addAtIndex(index,val)
 * obj.deleteAtIndex(index)
 */

let list1 = new MyLinkedList();
list1.addAtHead(4)
list1.addAtTail(3)
list1.deleteAtIndex(1)
list1.addAtTail(5)
list1.addAtIndex(1,1)
list1.addAtIndex(2,6)
list1.deleteAtIndex(2)
list1.deleteAtIndex(1)
list1.addAtHead(7)
list1.get(3)
</script>
</html>